package com.zmx.job.execute.handler.b2c.order.service;

import com.zmx.job.execute.handler.b2c.order.entity.B2COrder;
import com.zmx.job.execute.handler.b2c.order.entity.B2COrderItem;
import com.zmx.job.execute.handler.b2c.order.entity.B2COrderLog;
import com.zmx.job.execute.handler.b2c.order.entity.B2COrdercancel;
import com.zmx.job.execute.handler.b2c.order.repository.B2COrderItemRepository;
import com.zmx.job.execute.handler.b2c.order.repository.B2COrderLogRepository;
import com.zmx.job.execute.handler.b2c.order.repository.B2COrderRepository;
import com.zmx.job.execute.handler.b2c.order.repository.B2COrdercancelRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

/**
 * @Description: 订单实现类
 * @ClassName: OrderService
 * @Author zhaomxr
 * @Date 2021-10-22 15:00
 */
@Slf4j
@Service
public class OrderService {

    @Autowired
    private B2COrderRepository b2COrderRepository;
    @Autowired
    private B2COrdercancelRepository b2COrdercancelRepository;
    @Autowired
    private B2COrderLogRepository b2COrderLogRepository;
    @Autowired
    private OrderService orderService;

    /**
     * 取消超时未支付订单，直接取消
     */
    @Transactional(rollbackFor = Exception.class)
    public void overdueCancelOrder(B2COrder b2COrder) {
        try {
            //记录订单取消队列
            B2COrdercancel cancelOrder = new B2COrdercancel();
            cancelOrder.setId(b2COrder.getId());
            cancelOrder.setStatus(1);
            cancelOrder.setOrders(b2COrder.getId());
            cancelOrder.setOrderSn(b2COrder.getOrderSn());
            cancelOrder.setCreateTime(new Date());
            cancelOrder.setCreator("sys_cancel");
            cancelOrder.setSource(2);
            cancelOrder.setReason("no pay than 30 minute");
            cancelOrder.setTs(new Date());
            cancelOrder.setDr(0);
            b2COrdercancelRepository.save(cancelOrder);

            //记录取消订单日志
            orderService.saveOrderLog(b2COrder);

            //取消订单，更新订单状态为已取消
            b2COrderRepository.updateOrderStatusById(b2COrder.getId(), 3);

            log.info("订单：{} 已取消", b2COrder.getOrderSn());
        } catch (Exception e) {
            log.error("系统自动取消订单失败 订单code：{} ,{}" + b2COrder.getOrderSn(), e);
            //回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    /**
     * 记录取消订单日志
     */
    @Async
    public void saveOrderLog(B2COrder b2COrder) {
        B2COrderLog b2COrderLog = new B2COrderLog();
        b2COrderLog.setOrders(b2COrder.getId());
        b2COrderLog.setType(0);
        b2COrderLog.setOperator("sys_cancel");
        b2COrderLog.setContent("no pay than 30 minute");
        b2COrderLog.setCreateTime(new Date());
        b2COrderLog.setModifyTime(new Date());
        b2COrderLog.setDr(0);
        b2COrderLog.setTs(new Date());
        b2COrderLogRepository.save(b2COrderLog);
    }
}
